package iterator;

import index.ColumnarIndexScan;
import index.IndexException;
import index.UnknownIndexTypeException;

import java.io.IOException;
import java.util.HashMap;

import bufmgr.PageNotReadException;
import columnar.ColumnarFileInfo;
import columnar.Columnarfile;
import columnar.TupleScan;
import global.AttrType;
import global.Convert;
import global.IndexType;
import global.RID;
import global.Size;
import global.TID;
import heap.Heapfile;
import heap.InvalidTupleSizeException;
import heap.InvalidTypeException;
import heap.Scan;
import heap.Tuple;




public class ColumnarDupliElim {
	private static  AttrType[] _in;     // memory for array allocated by constructor
	  private short       in_len;
	  private short[]    str_lens;
	  
	  private TupleScan _am;
	  private boolean      done;
	  private Columnarfile f;
	  private AttrType  sortFldType;
	  private int       sortFldLen;
	  private Tuple    Jtuple;
	  private Heapfile []hf;
	  private Tuple TempTuple1, TempTuple2;
	  private static Iterator am;
	  private String cfile;
	  private TID tid;
	
	public ColumnarDupliElim (AttrType[] in,
			short len_in,
			short[] s,
			java.lang.String ColumnarFileName,
			int amt_of_mem,
			boolean inp_sorted)
			
			{

		       _in=in;
		       in_len=len_in;
		       str_lens=s;
		       f= new Columnarfile(ColumnarFileName,len_in,in);
		      // System.out.println(ColumnarFileName);
		       
		        //cInfo = new  ColumnarFileInfo(ColumnarFileName+".hdr");
				
		        ColumnarFileInfo cInfo = f.getColumnarFileInfo(ColumnarFileName+".hdr");
		        //String[] ColNames=cInfo.getColumnNames();
		    	 //Heapfile curHeap=f.getHeapfileForColumname(ColNames[0]);*/
		    	   
		    	_am= new TupleScan(f);
				
				tid=new TID();
				tid.recordIDs = new RID[len_in];
				for(int i=0; i<len_in; i++)
					tid.recordIDs[i] = new RID();
				
			}
	
	
//	@SuppressWarnings("null")
	public void ElimDupl()
	{
		
		int index=0;
		Tuple TempTuple= new Tuple();
		TupleScan ts;
		HashMap<String, Integer> hm= new HashMap<String, Integer>();
		System.out.println("In elimination function......");
			 
		
		
		
		//try {
			
		    TempTuple=_am.getNext(tid);
		    
		    
		    
			while( TempTuple!= null)
			{
				String tupledata = new String();
				tupledata = "";
				int value, fldcnt = 1, offset = 0;
			    String column=null;
				try {
				    for(AttrType attr: _in)
					{
						if(attr.attrType == AttrType.attrInteger)
						{
							value = TempTuple.getIntFld(fldcnt++);
							//Convert.setIntValue(value, offset, tupledata);
							tupledata = tupledata+Integer.toString(value);
							//offset = offset + 4;
						}
						else if (attr.attrType == AttrType.attrString)
						{
							//System.out.println("offset: "+offset);
							column = TempTuple.getStrFld(fldcnt++);
							//Convert.setStrValue(column, offset, tupledata);
							tupledata = tupledata+column;
							//offset = offset + Size.STRINGSIZE;
						}
					}
				    }catch(Exception e)
				    {
				    	e.printStackTrace();
				    }
				    try{
				    TempTuple.print(_in);}
				    catch(Exception e){}
				//Insert tuples in a HashMap...
				if(!hm.containsKey(tupledata))
					{ 
					  // Duplicate[index++]=TempTuple;
					System.out.println("put");
					hm.put(tupledata, 1);
					System.out.println(tupledata);   
					   
					 //  hm.equals(arg0)
					
					}
				else
					{ 
						System.out.println("not put");
					 System.out.println(tupledata);
					  
					}
				
				TempTuple= _am.getNext(tid);
				
			}
		/*} catch (JoinsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidTupleSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidTypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PageNotReadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TupleUtilsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PredEvalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SortException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LowMemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknowAttrType e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownKeyTypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
	/*	try {
		//	am.close();
		} catch (JoinsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SortException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
*/	//	return Duplicate;
		
		
	}

	

}
